﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using vensoft.Entities;
using vensoft.DataAccess;

namespace vensoft.DataAccess
{
    public class PersonalDataAccess:MasterDataAcess,IPersonalDataAccess
    {
        public PersonalDataAccess()
            : base()
        { }

        public List<Entities.Personal> GetAllFromPersonal()
        {
            SqlCommand comand = base.CreateSelectCommand("GetAllFromPersonal");
            return SelectObjectFactory(comand);
        }

        public List<Personal> SelectObjectFactory(SqlCommand comand)
        {
            List<Personal> lista = new List<Personal>();
            using (SqlDataReader reader = base.ExecuteReader(comand))
            {
                while (reader.Read())
                {
                    Personal personal =
                        new Personal()
                        {
                            Cod_Personal=reader.GetInt32(0),
                            dni = reader.GetString(1),
                            nombre = reader.GetString(2),
                            ap_pat_per = reader.GetString(4),
                            ap_mat_per = reader.GetString(3),
                            direccion = reader.GetString(6),
                            telefono = reader.GetString(5),
                            cargo = reader.GetString(7),
                        };
                    lista.Add(personal);
                }
            }
            return lista;
        }

        public List<Personal> GetFromPersonalByDescripcion(string criterio)
        {
            SqlCommand comand = base.CreateSelectCommand("GetFromPersonalByDni");
            comand.Parameters.AddWithValue("@DNI", criterio);
            return SelectObjectFactory(comand);
        }

        public Personal GetPersonalByDni(string dni)
        {
            throw new NotImplementedException();
        }

        public void RegistrarPersonal(Personal personal)
        {
            SqlCommand command = base.CreateCommand("RegistrarPersonal");
            AddParameters(personal, command);            
        }

        private void AddParameters(Personal personal, SqlCommand command)
        {
            base.AddInParameter(command, "@Cod_Personal", personal.Cod_Personal);
            base.AddInParameter(command, "@DNI", personal.dni);
            base.AddInParameter(command, "@Nombre", personal.nombre + ' ' + personal.ap_pat_per + ' ' + personal.ap_mat_per);
            base.AddInParameter(command, "@AP_Materno", personal.ap_mat_per);
            base.AddInParameter(command, "@AP_Paterno", personal.ap_pat_per);
            base.AddInParameter(command, "@Telefono", personal.telefono);
            base.AddInParameter(command, "@Direccion", personal.direccion);
            base.AddInParameter(command, "@Cargo", personal.cargo);

            base.ExecuteCommand(command);
        }

        public void EliminarPersonal(Personal personal)
        {
            SqlCommand comand = base.CreateSelectCommand("EliminarPersonal");
            base.AddInParameter(comand, "@Cod_Personal", personal.Cod_Personal);
            base.ExecuteCommand(comand);
        }


        public Personal GetFromPersonalByDNI(string dni)
        {
            SqlCommand command = base.CreateCommand("GetFromPersonalByDNI");
            base.AddInParameter(command, "dni", dni);

            Personal personal = null;

            if (SelectObjectFactory(command).Count > 0)
            {
                personal = SelectObjectFactory(command).First();
            }

            return personal;
        }

        public List<Personal> RetornarPersonalSinUsuario()
        {
            SqlCommand comand = base.CreateSelectCommand("RetornarPersonalSinUsuario");
            return SelectObjectFactory(comand);
        }

        public List<Personal> GetFromPersonalByDescNoAct(string criterio)
        {
            SqlCommand comand = base.CreateSelectCommand("GetFromPersonalByDescNoActivados");
            comand.Parameters.AddWithValue("@DNI", criterio);
            return SelectObjectFactory(comand);
        }
        
        public void ActivarUsuario(int cod_pers)
        {
            SqlCommand command = base.CreateCommand("ActivarPersonal");
            base.AddInParameter(command, "cod_pers", cod_pers);
            base.ExecuteCommand(command);
        }
    }
}
